package
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class ScreenRegistration extends Screen
	{
		
		private var skin:ScreenRegistrationSkin;
		public var arrCapes:Array = ["cape1", "cape2", "cape3"];
		public var arrHelms:Array = ["helm1", "helm2", "helm3"];
		public var arrArmors:Array = ["armor1", "armor2", "armor3"];
		public var arrOffhands:Array = ["oh1", "oh2", "oh3"];
		public var arrWeapons:Array = ["weapon1", "weapon2", "unarmed"];
		public var strGender:String;
		
		public function ScreenRegistration()
		{
			
			super();
			this.strGender = "Male";
			
		}
		
		public override function init():void
		{
			
			this.skin = new ScreenRegistrationSkin();
			addChild(this.skin);
			
				if (this.skin == null)
				{
					
					assetClass = getDefinitionByName("ScreenRegistrationSkin") as Class;
					this.skin.btnMale = this.skin.getChildByName("btnMale") as MovieClip;
					this.skin.btnFemale = this.skin.getChildByName("btnFemale") as MovieClip;
					this.skin.name_desiredTxt = this.skin.getChildByName("name_desiredTxt") as TextField;
					this.skin.selectedCape_txt = this.skin.getChildByName("selectedCape_txt") as TextField;
					this.skin.selectedWeapon_txt = this.skin.getChildByName("selectedWeapon_txt") as TextField;
					this.skin.selectedOffhand_txt = this.skin.getChildByName("selectedOffhand_txt") as TextField;
					this.skin.selectedHelm_txt = this.skin.getChildByName("selectedHelm_txt") as TextField;
					this.skin.selectedArmor_txt = this.skin.getChildByName("selectedArmor_txt") as TextField;
					this.skin.arrowRHair = this.skin.getChildByName("arrowRHair") as MovieClip;
					this.skin.arrowLHair = this.skin.getChildByName("arrowLHair") as MovieClip;
					this.skin.arrowRCape = this.skin.getChildByName("arrowRCape") as MovieClip;
					this.skin.arrowLCape = this.skin.getChildByName("arrowLCape") as MovieClip;
					this.skin.arrowRWeapon = this.skin.getChildByName("arrowRWeapon") as MovieClip;
					this.skin.arrowLWeapon = this.skin.getChildByName("arrowLWeapon") as MovieClip;
					this.skin.arrowROffhand = this.skin.getChildByName("arrowROffhand") as MovieClip;
					this.skin.arrowLOffhand = this.skin.getChildByName("arrowLOffhand") as MovieClip;
					this.skin.arrowRHelm = this.skin.getChildByName("arrowRHelm") as MovieClip;
					this.skin.arrowLHelm = this.skin.getChildByName("arrowLHelm") as MovieClip;
					this.skin.arrowRArmor = this.skin.getChildByName("arrowRArmor") as MovieClip;
					this.skin.arrowLArmor = this.skin.getChildByName("arrowLArmor") as MovieClip;
					this.skin.cancelBtn = this.skin.getChildByName("cancelBtn") as MovieClip;
					this.skin.acceptBtn = this.skin.getChildByName("acceptBtn") as MovieClip;
					this.skin.cpEye = this.skin.getChildByName("cpEye") as ColorPicker;
					this.skin.cpArmorTrim = this.skin.getChildByName("cpArmorTrim") as ColorPicker;
					this.skin.cpArmorBase = this.skin.getChildByName("cpArmorBase") as ColorPicker;
					this.skin.cpSkin = this.skin.getChildByName("cpSkin") as ColorPicker;
					this.skin.cpHair = this.skin.getChildByName("cpHair") as ColorPicker;
					this.skin.other_alt_txt_name = this.skin.getChildByName("other_alt_txt_name") as TextField;
					this.skin = addChildAt(new assetClass(), 0) as MovieClip;
					
				}
				
				this.skin.acceptBtn.addEventListener(MouseEvent.CLICK, onAccept);
				this.skin.cancelBtn.addEventListener(MouseEvent.CLICK, onCancel);
				
				var sides:Array = ["R", "L"];
				var typs:Array = ["Hair", "Cape", "Weapon", "Offhand", "Helm", "Armor"];
				var i:int = 0;
				
					while (i < typs.length)
					{
						
						var s:String = typs[i].split(",") as String;
						["arrow" + sides[0] + s].addEventListener(MouseEvent.CLICK, onRightScrollItem);
						["arrow" + sides[1] + s].addEventListener(MouseEvent.CLICK, onLeftScrollItem);
						i++;
						
					}
					
				var ci:int = 0;
				var cps:Array = ["ArmorTrim", "Eye", "ArmorBase", "Skin", "Hair"];
				
					while (ci < cps.length)
					{
						
						["cp" + cps[ci] as String].addEventListener(ColorPickerEvent.SELECT, onCPSelect);
						ci++;
						
					}
					
					this.gender = strGender;
					
		}
		
		public function get gender():String
		{
			
			return this.strGender == this.skin.getChildByName("btn" + this.strGender) ? this.strGender : "Male";
			
			
		}
		
		public function set gender(sGen:String):void
		{
			
			var i:int = 0;
			var choices:Array = ["Male", "Female"];
			
				while (i < choices.length)
				{
					
					this.gender = sGen.indexOf(choices[i]) > -1;
					i++;
					
				}
		}
		
		public function isCreationValid():Boolean
		{
			
			return this.skin.name_desiredTxt.length > 0 && this.skin.other_alt_txt_name.length > 0 && this.gender.length > 0 && this.gender != null ? true : false;
			
		}
		
		private function onRightScrollItem(event:MouseEvent):void
		{
			
			var mc:MovieClip = event.currentTarget as MovieClip;
			var s:String = mc["name"];
			var i:int = 0;
				
				if (s.length > 0 && !(mc == null))
				{
					
				s = s.split(s.substr(0, 3));
					
					while (i < ["arr" + s + "S"].length)
					{
						
						
					}
				
				
		
		private function onAccept(event:MouseEvent):void
		{
			
			var bValid:Boolean = this.isCreationValid();
			var popup:DialoguePopup;
			
				if (bValid)
				{
					
					Game.loginRedirect();
					
				} else {
					
					Utility.traceMessage("Oops! Something went wrong with the registration process!!");
					popup = new DialoguePopup().init();
					
				}
		}
		
		private function onDecline(event:MouseEvent):void
		{
			
			Game.loginRedirect();
			
		}
	}
}
				
	